﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Linq;
using System.Text;

using ZhimeiSoft.Framework.Client.Utils;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public class ServiceItemCCtrl
    {
        public DBEntityServiceItem GetServiceItem(string sKeyword)
        {
            if (String.IsNullOrEmpty(sKeyword))
                return null;

            DBEntityServiceItem serviceItemInfo = null;
            string sFilter = string.Format("( {0} LIKE '%{1}%' OR {2} LIKE '%{1}%') ", DBEntityServiceItem.FieldName.SNAME, sKeyword, DBEntityServiceItem.FieldName.SERVICEITEMCODE);
            string error = string.Empty;
            DataSet ds = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryServiceItem(sFilter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取服务项目失败\r\n" + error);
                return null;
            }

            if (ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                serviceItemInfo = new DBEntityServiceItem(dr);
            }
            else if (ds.Tables[0].Rows.Count > 1)
            {
                frmServiceItemList form = new frmServiceItemList(ds);
                if (form.ShowDialog() == DialogResult.OK)
                {
                    DataRow dr = form.SelectedRow;
                    serviceItemInfo = new DBEntityServiceItem(dr);
                }
            }
            return serviceItemInfo;
        }

        public DBEntityCustomerServiceItem GetCustomerServiceItem(int iCustomerID, string sKeyword)
        {
            if (iCustomerID < 0)
                return null;

            DBEntityCustomerServiceItem customerServiceItemInfo = null;
            string sFilter = string.Format("{0}={1}", DBEntityCustomerServiceItem.FieldName.CUSTOMERID, iCustomerID.ToString());
            if (!string.IsNullOrEmpty(sKeyword))
                sFilter += string.Format(" AND ( {0} LIKE '%{1}%' OR {2} LIKE '%{1}%') ", DBEntityServiceItem.FieldName.SNAME, sKeyword, DBEntityServiceItem.FieldName.SERVICEITEMCODE);
            string error = string.Empty;
            DataSet ds = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(sFilter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员服务项目信息失败\r\n" + error);
                return null;
            }
            if (ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                customerServiceItemInfo = new DBEntityCustomerServiceItem(dr);
            }
            else if (ds.Tables[0].Rows.Count > 1)
            {
                frmServiceItemList form = new frmServiceItemList(ds);
                if (form.ShowDialog() == DialogResult.OK)
                {
                    DataRow dr = form.SelectedRow;
                    customerServiceItemInfo = new DBEntityCustomerServiceItem(dr);
                }
            }
            return customerServiceItemInfo;

        }

        public static DBEntityCustomerServiceItem GetCustomerServiceItem(int iCustomerID, int iServiceItemID)
        {
            DBEntityCustomerServiceItem entity = null;
            string error = string.Empty;
            DataSet ds = new DataSet();
            DBEntityCustomerServiceItem item = new DBEntityCustomerServiceItem();
            
            string sFilter = string.Format("{0}={1} AND {2}.{3}={4}", 
                DBEntityCustomerServiceItem.FieldName.CUSTOMERID, iCustomerID, 
                item.GetTableName(),
                DBEntityCustomerServiceItem.FieldName.SERVICEITEMID, iServiceItemID);
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(sFilter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员服务项目信息失败\r\n" + error);
                return null;
            }
            if (ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                entity = new DBEntityCustomerServiceItem(dr);
            }
            return entity;
        }

        public static DBEntityCustomerServiceItem GetCustomerServiceItemByIndexCode(int iCustomerID, string sIndexCode)
        {
            DBEntityCustomerServiceItem entity = null;
            string error = string.Empty;
            DataSet ds = new DataSet();

            //每次都弹出框
            string filter = string.Format(" {0} = {1} ", DBEntityCustomerServiceItem.FieldName.CUSTOMERID, iCustomerID.ToString());
            if (!string.IsNullOrEmpty(sIndexCode))
            {
                filter += string.Format(" AND {0} like '%{1}%'", DBEntityServiceItem.FieldName.SINDEXCODE, sIndexCode);
            }
            bool bFlag = BussinessProxyManager.Instance.QueryCustomerServiceItem(filter, out ds, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取会员服务项目信息失败\r\n" + error);
                return null;
            }

            if (ds == null || ds.Tables.Count <= 0)
                return null;

            if (!string.IsNullOrEmpty(sIndexCode) && ds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                entity = new DBEntityCustomerServiceItem(dr);
            }
            else
            {
                frmServiceItemList form = new frmServiceItemList(ds);
                if (form.ShowDialog() == DialogResult.OK)
                {
                    DataRow dr = form.SelectedRow;
                    entity = new DBEntityCustomerServiceItem(dr);
                }
            }

            return entity;
        }
    }
}
